package com.own.business.resource.module.aliyun;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.own.business.resource.common.constant.ConstantCommon;
import com.own.business.resource.entity.po.FileRecord;
import lombok.extern.slf4j.Slf4j;

/**
 * AliYunResourceUtil
 *
 * @author chenxueli
 * @date 2024-03-27 09:48:00
 */
@Slf4j
public class AliYunResourceUtil {

    /**
     * 处理图片尺寸
     */
    public static String getResourceDimension(FileRecord fileRecord) {
        // 如果不是图片信息直接返回
        if (!ConstantCommon.IMAGE_FORMAT.contains(fileRecord.getType())) {
            return "";
        }
        // 获取请求的地址
        try {
            var url = fileRecord.getPosition() + fileRecord.getPath();
            url = url + (url.contains("?") ? "&" : "?") + "x-oss-process=image/info";
            var body = HttpRequest.get(url).execute().body();
            var object = JSON.parseObject(body);
            var width = object.getJSONObject("ImageWidth").getInteger("value");
            var height = object.getJSONObject("ImageHeight").getInteger("value");
            return width + "x" + height;
        } catch (Exception e) {
            return "";
        }
    }

}
